package vn.gq.tym.model;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import vn.gq.tym.model.bean.ChapterItem;
import vn.gq.tym.truyennguoilonfull.R;
import vn.gq.tym.utils.Client;
import vn.gq.tym.utils.DataType;
import vn.gq.tym.utils.IClientListener;
import vn.gq.tym.utils.OnNetworkListener;
import vn.gq.tym.widget.BaseFragment;
import android.app.Activity;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class ChapterAdapter extends BaseAdapter {

	private LayoutInflater mInfalter;
	private Activity activity;
	private List<ChapterItem> mData = new ArrayList<ChapterItem>();
	private BaseFragment mFragment;
	
	public ChapterAdapter(Activity activity, BaseFragment fragment) {
		mInfalter = LayoutInflater.from(activity);
		this.activity = activity;
		mFragment = fragment;
	}
	
	public String getFileUrl(String fileName) {
		return String.format("http://data.toiyeumobile.org/truyen-nguoi-lon-full/data", fileName);
	}
	
	public void load(final String fileName, final Runnable run) {
		Client client = new Client(activity);
		client.setOutputDataType(DataType.GZIP_CIPHER_JSON);
		client.executeAndListener(getFileUrl(fileName), new IClientListener() {
			
			@Override
			public void onDone(int code, Object result) {
				if (code == 1) {
					if (mFragment instanceof OnNetworkListener) {
						((OnNetworkListener) mFragment).onNoNetwork();
					}
				}
				if (result != null && result instanceof JSONArray) {
					JSONArray obj = (JSONArray) result;
					try {
						for (int i=0;i<obj.length();i++) {
							JSONObject jsItem = obj.getJSONObject(i);
							ChapterItem chapterItem = new ChapterItem();
							chapterItem.setUrl(jsItem.getString("id"));
							chapterItem.setTitle(jsItem.getString("title"));
							mData.add(chapterItem);
						}
					} catch(JSONException e) {
						e.printStackTrace();
					}
				}
				activity.runOnUiThread(run);
			}
		});
	}
	
	@Override
	public int getCount() {
		return mData.size();
	}

	@Override
	public Object getItem(int position) {
		return mData.get(position);
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}
	
	public ArrayList<ChapterItem> getData() {
		return (ArrayList<ChapterItem>) mData;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			convertView = mInfalter.inflate(R.layout.list_item_layout, parent, false);
			holder = new ViewHolder();
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.titleTextView = (TextView) convertView.findViewById(R.id.itemlist);
		Typeface tf = Typeface.createFromAsset(activity.getAssets(), "fonts/Roboto-Condensed.ttf");
		holder.titleTextView.setTypeface(tf);
		ChapterItem chapterItem = (ChapterItem) getItem(position);
		holder.titleTextView.setText(chapterItem.getTitle());
		return convertView;
	}
	
	private class ViewHolder {
		public TextView titleTextView;
	}

}
